package com.example.dubbosummer.config;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.example.dubboprider.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DubooConfig {
    @Autowired
    ApplicationConfig applicationConfig;
//    @Autowired
//    private  ApplicationContext applicationContext;
//    @Autowired
//    StaticListableBeanFactory staticListableBeanFactory;
    @Autowired
    RegistryConfig registryConfig;
    @Bean()
    public ProductService getProductService(){
//        ConfigurableApplicationContext configurableContext = (ConfigurableApplicationContext) applicationContext;
//        BeanDefinitionRegistry beanDefinitionRegistry = (DefaultListableBeanFactory) configurableContext.getBeanFactory();
//        System.out.println(beanDefinitionRegistry);
//
//        BeanDefinitionBuilder beanDefinitionBuilder =
//                BeanDefinitionBuilder.genericBeanDefinition("com.example.dubboprider.service.ProductService");
//        BeanDefinition beanDefinition=beanDefinitionBuilder.getBeanDefinition();
//        beanDefinitionRegistry.registerBeanDefinition("productService",beanDefinition);
        ReferenceConfig<ProductService> reference = new ReferenceConfig<ProductService>(); // 此实例很重，封装了与注册中心的连接以及与提供者的连接，请自行缓存，否则可能造成内存和连接泄漏
        reference.setApplication(applicationConfig);
        reference.setRegistry(registryConfig); // 多个注册中心可以用setRegistries()
        reference.setInterface(ProductService.class);
        reference.setVersion("1.0.0");
        ProductService productService = reference.get();
        return productService;
    }
}
